#encoding:UTF-8
import mylib
import re
import MySQLdb
import pythonlibSB

conn = mylib.create_conn()


def get_ketqua():
    results = get_url_ketqua()
    url,id_giai,url_time = results[0],int(results[1]),results[2]
    content_time = mylib.get_datetime_today()
    content = mylib.get_data(url)
    content = pythonlibSB.s_string(content,'id="table2"','table')
    last_str = ""
#    print content
    pos1 = content.find('<tr')
    while pos1 != -1:
        pos2 = content.find('</tr',pos1)
        if pos2 == -1:
            break
        t = content[pos1:pos2]
        reg = re.compile('<br />')
        for match in reg.findall(t):
            t = t.replace(match,'</td')
        new_str = "<p>"
        for d in re.finditer('(<td)(.*?)(>)(.*?)(</td)',t):
            data = d.group(4)
            data = pythonlibSB.remove_tag(data)
            new_str = new_str + data +" "
        new_str = new_str + "</p>"
        last_str = last_str + new_str
        pos1 = pos2 + 1
    
    print last_str
    last_str = pythonlibSB.chuanHoaHtml(last_str)
    insert_ketqua(id_giai, url_time, last_str, content_time)
    print "xong"

def insert_ketqua(id_giai,url_time,content,content_time):
    try:
        content = MySQLdb.escape_string(content)
        cmd = "CALL insert_ketqua('%s','%s','%s','%s')"%(id_giai,url_time,content,content_time)
        print cmd
        cursor = conn.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute(cmd)
        cursor.close()
        print "insert thanh cong"
    except:
        print "loi insert SQL"


def get_url_ketqua():
    try:
        cmd = "SELECT * FROM tbl_ketqua WHERE `status` = 0"
        url_get = mylib.get_result_sql(conn,cmd)
        urls = url_get[0]
        url = urls['url_giai']
        id_giai = urls['id_giai']
        url_time = urls['url_time']
        
        return (url,id_giai,url_time)
    except:
        return ()


if __name__ == "__main__":
    get_ketqua()